﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using BrokerBase.Lib.Company;
using Broker.View.ViewModel;
using BrokerBase.Lib.Product;
using NHibernate;
using BrokerBase.Lib.Policy;
using BrokerBase.Lib.Person;
using BrokerBase.Lib.Tool;
using Broker.View.PolicyView;

namespace Broker.View.ProductView
{
    /// <summary>
    /// ProductListPage2.xaml 的交互逻辑
    /// </summary>
    public partial class ProductListPage2 
    {
        private IList<IProduct> products;

        public ProductListPage2()
        {
            InitializeComponent();

            Loaded += new RoutedEventHandler(ProductListPage2_Loaded);
        }

        void ProductListPage2_Loaded(object sender, RoutedEventArgs e)
        {
            if (products == null)
            {
                LoadProduct(txtSearch.Text.Trim());
            }
        }

        private void LoadProduct(string likeName)
        {
            try
            {
                ISession sess = LibGlobal.HbmSessionFactory.GetCurrentSession();

                IQuery query = null;

                string hql = "from Product p where 1=1";

                if (string.IsNullOrEmpty(likeName))
                {
                    query = sess.CreateQuery(hql);
                }
                else
                {
                    hql +=" and p.Name like :name";
                    query =sess.CreateQuery(hql).SetString("name", string.Format("%{0}%",likeName));
                }

                IList<IProduct> result = query.List<IProduct>();
                
                products = result.OrderBy(p => p.Name).AsParallel().ToList<IProduct>();

                gvProduct.ItemsSource = products;

                tblInfo.Text = "保险产品: " + products.Count + "个";

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void gvProduct_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            IProduct product = gvProduct.SelectedItem as IProduct;

            if (product == null)
            {
                MessageBox.Show("请选择保险产品", "提示");
            }
            else if (product.ProductType == ProductType.Extra)
            {
                MessageBox.Show("产品演示请先选择主险,", "提示");
            }
            else if (product.ProductType == ProductType.Basic)
            {
                DemoView(product);
            }
        }

        private void btnDemo_Click(object sender, RoutedEventArgs e)
        {
            IProduct product = gvProduct.SelectedItem as IProduct;

            if (product == null)
            {
                MessageBox.Show("请选择保险产品", "提示");
            }
            else if (product.ProductType == ProductType.Extra)
            {
                MessageBox.Show("产品演示请先选择主险,", "提示");
            }
            else if (product.ProductType == ProductType.Basic)
            {
                DemoView(product);
            }
        }

        private void btnProduct_Click(object sender, RoutedEventArgs e)
        {
            ProductView();
        }

        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            LoadProduct(txtSearch.Text.Trim());
        }

        private void ProductView()
        {
            IProduct product = gvProduct.SelectedItem as IProduct;

            if (product == null)
            {
                MessageBox.Show("请选择保险产品", "提示");
            }
            else
            {
                ProductForm frm = new ProductForm(product);

                frm.Owner = LibGlobal.MainWindow;
                frm.WindowStartupLocation = WindowStartupLocation.CenterOwner;
                frm.Show();
            }
        }

        private void DemoView(IProduct product)
        {
            

            IScheme scheme = new Scheme();
            scheme.BizDate = DateTime.Now;
            scheme.CreateDate = DateTime.Now;

            SexType defaultSex = LibGlobal.GetClientCfg().DefaultSex == "男" ? SexType.Male : SexType.Fmale;

            scheme.HolderSex = defaultSex;
            scheme.HolderAge = PersonUtil.GetAgeFromBirthdayAndTime(LibGlobal.GetClientCfg().DefaultBirthday, DateTime.Now);

            scheme.InsurantSex = defaultSex;
            scheme.InsurantAge = PersonUtil.GetAgeFromBirthdayAndTime(LibGlobal.GetClientCfg().DefaultBirthday, DateTime.Now);

            IPolicyLight pl = new PolicyLight(scheme, new Policy(scheme, ProductType.Basic));

            scheme.PolicyLights.Add(pl);

            Scheme2Form frm = new Scheme2Form(scheme, SchemeType.Fast, product);

            frm.WindowStartupLocation = WindowStartupLocation.CenterOwner;

            frm.Owner = LibGlobal.MainWin;
            frm.Show();


            
        }

        private void txtSearch_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                LoadProduct(txtSearch.Text.Trim());
            }
        }
    }
}
